<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>待处理订单</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称"
                v-model="name" suffix-icon="el-icon-search"></el-input>
      <el-input style="width: 200px" placeholder="请输入类型"
                v-model="username" suffix-icon="el-icon-user" class="ml-5 mr-5"></el-input>
      <el-button class="ml-5" type="primary" @click="search">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>
    <el-table :data="tableData" border stripe
              @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="40">
      </el-table-column>
      <el-table-column prop="id" label="id" width="40">
      </el-table-column>
      <el-table-column prop="goods.goodsName" label="货物名称" width="100">
      </el-table-column>
      <el-table-column prop="num" label="数量" width="100">
      </el-table-column>
      <el-table-column prop="businessType" label="业务类型" width="100">
      </el-table-column>
      <el-table-column prop="receiveCompany.companyName" label="收获公司" width="100">
      </el-table-column>
      <el-table-column prop="money" label="总金额" width="100">
      </el-table-column>
      <el-table-column prop="orderState" label="订单状态" width="100">
        <template slot-scope="scope">
          {{ scope.row.orderState|state }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)" class="mr-5">
            填写订单详情<i class="el-icon-edit"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size=pageSize
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>
    <!--    填写运输任务弹窗   -->
    <el-dialog title="详细信息" :visible.sync="dialogFormVisible" width="50%" :close-on-click-modal="false">
      <el-form :model="trans" :rules="rules" ref="trans">
        <el-form-item label='订单id' label-width="120px" prop="id">
          <el-input v-model="trans.orderId" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="运输方式:" label-width="120px" prop="type" placeholder="请选择">
          <el-select v-model="trans.transType" @change="typeChange">
            <el-option value="0" label="汽车运输"></el-option>
            <el-option value="1" label="铁路运输"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="trans.transType" label="路线选择" label-width="120px" prop="route">
          <el-select value="route" v-model="route" placeholder="请选择路线" value-key="id" @change="routeChange">
            <el-option
                v-for="route in routes"
                :value="route"
                :key="route.id"
                :label="route.startPlace+'--至--'+route.endPlace+'--专线名称--:'+route.dedicatedLine"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专线名称" label-width="120px" prop="name">
          {{route.dedicatedLine}}
        </el-form-item>
        <el-form-item v-if="route.type===1" label="火车车次" label-width="120px" prop="name">
          <el-input v-model="trans.trainName" autocomplete="off" placeholder="请填写火车车次"></el-input>
        </el-form-item>
        <el-form-item v-else-if="route.type===0" label="选择车辆" label-width="120px" prop="name">
          <el-select value="vehicleId" v-model="trans.vehicleId" placeholder="请选择车辆">
            <el-option
                v-for="vehicle in vehicles"
                :value="vehicle.id"
                :key="vehicle.id"
                :label="vehicle.vehicleId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" label-width="120px" prop="name">
          <el-input v-model="trans.remark" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button type="primary" @click="save('trans')">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "TransportTask",
  data() {
    return {
      tableData: [],
      currentPage: 1,
      name: "",
      username: "",
      gender: "",
      pageSize: 5,
      total: 0,
      dialogFormVisible: false,
      trans: {},
      route: {},
      routes: [],
      vehicle: {},
      vehicles: [],
      endPlace: '',
      order:{},
      rules: {
        // password: [
        //   {required: true, message: '请填写该字段', trigger: 'blur'},
        // ],
        // phoneNumber: [
        //   {required: true, message: '请输入电话号码', trigger: 'blur'},
        //   {min: 11, max: 11, message: '请输入长度为11个数字的电话号码', trigger: 'blur'}
        // ],
      },
    }
  },
  filters: {
    state(s) {
      if (s) {
        return '已完成'
      } else {
        return '未完成'
      }
    },
    showType(s) {
      if (s) {
        return '铁路'
      } else {
        return '汽运'
      }
    }
  },
  created() {
    this.load()
    request.get("/route").then(res => {
      this.routes = res.data
    })
    request.get("/vehicleInfo/get",).then(res => {
      this.vehicles = res.data
    })
  },
  methods: {
    load() {
      request.get("/orderToDo",
          {params: {pageNum: this.currentPage, pageSize: this.pageSize}}
      ).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.currentPage = res.data.current
        this.pageSize = res.data.size
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    routeChange(route) {
      console.log(route)
      this.route=route
    },
    typeChange(type){
      console.log(type)
      this.route={}
      request.get('routes/vehicle',{
        params:{startPlace:'四川省/攀枝花市/市辖区',
          endPlace:this.endPlace,
          type:type
        }}).then(res=>{
          this.routes=res.data
      })
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },
    handleEdit(row) {
      this.dialogFormVisible = true
      // this.order = Object.assign({}, row)
      this.trans.orderId = row.id
      this.endPlace=row.receiveCompany.companyAdress
    },
    save(trans) {
      this.$refs[trans].validate((valid) => {
        if (valid) {
          this.trans.transType = this.route.type
          this.trans.route = this.route.id
          console.log(this.trans)
          this.dialogFormVisible = false
          request.put("orderToDo", this.trans).then(res => {
            if (res) {
              this.$message.success("订单提交成功")
              this.load()
            } else {
              this.$message.error("订单提交失败")
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleSelectionChange(val) {
      // console.log(val)
      this.multipleSelection = val
    },
    search() {
      if (this.gender === " ") {
        this.gender = ""
      }
      request.get("/trans/selectbycondition", {
        params: {
          name: this.name,
          username: this.username,
          gender: this.gender,
        }
      }).then(res => {
        this.tableData = res
        this.total = this.tableData.length
        this.currentPage = 0
      })
    },
    reset() {
      this.username = ""
      this.name = ""
      this.gender = ""
      this.load()
    },
  }
}
</script>

<style scoped>

</style>